//编辑客户

interface IClientManagentEditViewModel extends ng.IScope {
    /**
     * 客户单位名称
     */
    name: string;
    clientId:string;
    /**
     * 客服专员编号
     */
    serviceOfficer: EmployeeListItemResponse;
    /**
     * 客服专员下拉列表
     */
    serviceOfficerIdDropDown: EmployeeListItemResponse[];
    /**
     * 商务专员编号
     */
    businessOfficer: EmployeeListItemResponse;
    /**
     * 商务专员下拉列表
     */
    businessOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 省编号
     */
    provinceCode: string;
    /**
     * 市编号
     */
    cityCode: string;
    /**
     * 区编号
     */
    areaCode: string;
    /**
     * 详细地址
     */
    address: string;
    /**
     * 管理员账户
     */
    adminUserName: string;
    /**
     * 管理员密码
     */
    adminPassword: string;
    /**
     * 重复管理员密码
     */
    reAdminPassword: string;
    /**
     * 手机号
     */
    phoneNumber: string;
    /**
     * 客户类型
     */
    clientType:string;
    /**
     * 客户类型下拉
     */

    clientTypeList:ValueListItemResponse[]
    /**
     * 产品
     */
    products: string;
    /**
     * 产品包装方式
     */
    productPack: string;
    /**
     * 付款方式
     */
    paidWay: string;
    /**
     * 备注
     */
    remarks: string;
    /**
     * 点击事件
     */
    click: () => void;

    /**
     * 返回事件
     */
    backUrl:()=>void;
}

class ClientManagentEditController {
    id: string;
    serviceOfficerId: string;
    businessOfficerId: string;
    

    constructor(private $scope: IClientManagentEditViewModel, private clientService: IClientService, private employeeService: IEmployeeService,
        private $location: ng.ILocationService, private $ngBootbox: BootboxService, private $state: angular.ui.IStateService, private valueService:IValueService) {
        this.init();
    }

    init(): void {
        // debugger;
        this.id = this.$location.search().id;

        if(!this.id){
             this.id = JSON.parse(window.localStorage.getItem("client").toString()).clientId;
        }else{
            this.id = this.id;
        }
        this.$scope.clientTypeList = this.valueService.getClientTypeList().data;
        
        this.clientService.getDetail(this.id).then(result => {
            let client = {
                clientId:this.id,
                clientName: result.data.name
            }
            window.localStorage.setItem("client",JSON.stringify(client));

            this.$scope.name = result.data.name;
            this.serviceOfficerId = result.data.clientServiceOfficerId;
            this.businessOfficerId = result.data.businessOfficerId;
            this.$scope.provinceCode = result.data.province;
            this.$scope.cityCode = result.data.city;
            this.$scope.areaCode = result.data.area;
            this.$scope.address = result.data.address;
            this.$scope.adminUserName = result.data.adminUserName;
            this.$scope.adminPassword = result.data.adminPassword;
            this.$scope.reAdminPassword = result.data.reAdminPassword;
            this.$scope.phoneNumber = result.data.phoneNumber;
            this.$scope.clientType = result.data.clientType;
            
            this.$scope.products = result.data.products;
            this.$scope.productPack = result.data.productPack;
            this.$scope.paidWay = result.data.paidWay;
            this.$scope.remarks = result.data.remarks;
            this.$scope.click = this.editClient;
            this.loadServiceOfficerDropDown();
            this.loadBusinessOfficerDropDown();
        });

        this.$scope.backUrl = this.backUrl;
    }

    /**
     * 返回事件
     */
    backUrl=()=>{
        this.$state.go("app.client.client_Managent", { name:'clientController' });
    }

    /**
     * 填充客服专员下拉列表
     */
    loadServiceOfficerDropDown(): void {
        this.employeeService.getbytype(CommissionerType.CustomerService.toString(),0, -1).then(result => {
            this.$scope.serviceOfficerIdDropDown = result.data.data;
            result.data.data.forEach((item) => {
                if (item.id == this.serviceOfficerId) {
                    this.$scope.serviceOfficer = item;
                }
            });
        });
    }

    /**
     * 填充商务专员下拉列表
     */
    loadBusinessOfficerDropDown(): void {
        this.employeeService.getbytype(CommissionerType.BusinessAffairs.toString(), 0, -1).then(result => {
            this.$scope.businessOfficerDropDown = result.data.data;
            result.data.data.forEach((item) => {
                if (item.id == this.businessOfficerId) {
                    this.$scope.businessOfficer = item;
                }
            });
        });
    }

    editClient = () => {
        if (!$("#clientEditForm").valid()) { return };
        this.$ngBootbox.confirm("确定保存此数据吗？").then(e => {
            this.clientService.edit(
                this.id,
                this.$scope.name,
                this.$scope.serviceOfficer.id,
                this.$scope.businessOfficer.id,
                this.$scope.provinceCode,
                this.$scope.cityCode,
                this.$scope.areaCode,
                this.$scope.address,
                this.$scope.adminPassword,
                this.$scope.reAdminPassword,
                this.$scope.phoneNumber,
                this.$scope.clientType,
                this.$scope.products,
                this.$scope.productPack,
                this.$scope.paidWay,
                this.$scope.remarks
            ).then(result => {
                window.localStorage.setItem("clientId",result.data.extData);
                if(this.$state.current.name.indexOf('New')>-1){
                    this.$state.go("app.client.customerrepresentative_manageNew", {
                        id: this.id
                    });
                }else{
                    this.$state.go("app.client.customerrepresentative_manage", {
                        id: this.id
                    });
                }   
            });
        });
    }
}

angular.module("tms").controller("clientManagentEditController", ["$scope", "clientService", "employeeService", "$location", "$ngBootbox", "$state", "valueService", ClientManagentEditController]);